#!/bin/sh
# 安装管理Tomcat
App=tomcat
AppName=Tomcat
AppBase=/App
AppDir=$AppBase/$App
AppStartup=$AppDir/bin/startup.sh
AppSrcBase=/App/src
AppSrcFile=apache-tomcat-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///')

# 获取PID
fpid()
{
    AppPid=$(ps ax | grep "$AppDir" | grep -v "grep" | awk '{print $1}' 2> /dev/null)
}

# 查询状态
fstatus()
{
    fpid

    if [ ! -f "$AppStartup" ]; then
            echo "$AppName 未安装"
    else
        echo "$AppName 已安装"
        if [ -z "$AppPid" ]; then
            echo "$AppName 未启动"
        else
            echo "$AppName 正在运行"
        fi
    fi
}

# 删除
fremove()
{
    fpid

    if [ -z "$AppPid" ]; then
        if [ -d "$AppDir" ]; then
            rm -rf $AppDir
            if [ $? -eq 0 ]; then
                echo "删除 $AppName"
            fi
        else
            echo "$AppName 未安装"
        fi
    else
        echo "$AppName 正在运行" && exit 1
    fi
}

# 备份
fbackup()
{
    Day=$(date +%Y-%m-%d)
    BackupFile=$App-$Day.tgz

    if [ -d "$AppDir" ]; then
        cd $AppBase
        tar zcvf $BackupFile --exclude=logs/* --exclude=work/* --exclude=temp/* --exclude=conf/Catalina --exclude=webapps/* $App
        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"
    else
        echo "$AppName 未安装"
    fi
}

# 安装
finstall()
{
    test -d "$AppDir" && echo "$AppName 已安装" && exit 1
    fupdate && fcpconf
}

# 拷贝配置
fcpconf()
{
    cp -vf --backup=numbered $ScriptDir/server.xml $AppDir/conf
}

# 更新
fupdate()
{
    fbackup
    test -f "$AppStartup" && echo "$AppName 已安装"
    cd $AppSrcBase
    test -d "$AppSrcDir" && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    mv $AppSrcDir $AppDir

    if [ $? -eq 0 ]; then
        echo "$AppName 安装成功"
        rm -rf $AppDir/webapps/*
    else
        echo "$AppName 安装失败"
        exit
    fi
}

# 启动
fstart()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppName 正在运行"
    else
        rm -rf $AppDir/work/* $AppDir/temp/* $AppDir/conf/Catalina/
        $AppStartup
        if [ $? -eq 0 ]; then
            echo "启动 $AppName"
        else
            echo "$AppName 启动失败"
        fi
    fi
} 

# 重启
frestart()
{
    fpid
    test -n "$AppPid" && fstop && sleep 1
    fstart
}

# 停止
fstop()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppPid" | xargs kill -9
        if [ $? -eq 0 ]; then
            echo "终止 $AppName 进程"
        else
            echo "终止 $AppName 进程失败"
            exit
        fi
    else
        echo "$AppName 进程未运行"
    fi
}

# 切割日志
fcutlog()
{
    fpid

    Time=$(date +'%Y-%m-%d %H:%M:%S')
    Day=$(date -d '-1 days' +'%Y-%m-%d') 

    echo "$Time"
    mv -vf --backup=numbered $AppDir/logs/catalina.out $AppDir/logs/catalina.$Day.out && echo "切割 $AppName 日志"
    frestart
}

ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    "install"   ) finstall;;
    "update"    ) fupdate;;
    "reinstall" ) fremove && finstall;;
    "remove"    ) fremove;;
    "backup"    ) fbackup;;
    "start"     ) fstart;;
    "stop"      ) fstop;;
    "restart"   ) frestart;;
    "status"    ) fstatus;;
    "cpconf"    ) fcpconf;;
    "cutlog"    ) fcutlog;;
    *           )
    echo "$ScriptFile install              安装 $AppName"
    echo "$ScriptFile update               更新 $AppName"
    echo "$ScriptFile reinstall            重装 $AppName"
    echo "$ScriptFile remove               删除 $AppName"
    echo "$ScriptFile backup               备份 $AppName"
    echo "$ScriptFile start                启动 $AppName"
    echo "$ScriptFile stop                 停止 $AppName"
    echo "$ScriptFile restart              重启 $AppName"
    echo "$ScriptFile status               查询 $AppName 状态"
    echo "$ScriptFile cpconf               拷贝 $AppName 配置"
    echo "$ScriptFile cutlog               切割 $AppName 日志"
    ;;
esac
